package com.itheima.dao;

import com.itheima.domain.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class StudentDao {
        /*
        * 查询所有
        * */

        public List<Student> selectAll()  {
                InputStream is = null;
                List<Student>  list = null;
                try {
                        is = Resources.getResourceAsStream("MyBatisConfig.xml");
                        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
                        SqlSession sqlSession = sqlSessionFactory.openSession();
                        list = sqlSession.selectList("StudentMapper.selectAll");

                } catch (IOException e) {
                        e.printStackTrace();
                }

                return list;
        }
        /*
        * 查询一个
        * */

        public void selectById () throws IOException {
                InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
                SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
                SqlSession sqlSession = sqlSessionFactory.openSession();
                com.itheima.domain.Student stu = sqlSession.selectOne("StudentMapper.selectById", 3);
                System.out.println(stu);
                sqlSession.close();
                is.close();
        }
        /*
        * 添加记录
        * */

        public int  insertStudent(Student student) {
                InputStream is = null;
                SqlSession sqlSession= null;
                try {
                        is = Resources.getResourceAsStream("MyBatisConfig.xml");
                        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
                        sqlSession = sqlSessionFactory.openSession(true);
                        int num = sqlSession.insert("StudentMapper.insertStudent",student);
                        return num;
                } catch (IOException e) {
                        e.printStackTrace();
                }
                if(sqlSession!=null){
                        sqlSession.close();
                }
                if(is!=null){
                        try {
                                is.close();
                        } catch (IOException e) {
                                e.printStackTrace();
                        }
                }
                return  -1;
        }
        /*
        * 删除记录
        * */

       public int deleteStudent(int sid)  {
                InputStream is = null;
               SqlSession sqlSession = null;
                try {
                        is = Resources.getResourceAsStream("MyBatisConfig.xml");
                        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
                        sqlSession = sqlSessionFactory.openSession(true);
                        int i = sqlSession.delete("StudentMapper.deleteStudent",sid);
                        return i;
                } catch (IOException e) {
                        e.printStackTrace();
                }
                if(sqlSession!=null){
                        sqlSession.close();
                }
               if(is!=null){
                       try {
                               is.close();
                       } catch (IOException e) {
                               e.printStackTrace();
                       }
               }
              return -1;
        }
        /*
        * 更新记录
        * */
        public void updateStudent() throws ParseException, IOException {
                InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
                SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
                SqlSession sqlSession = sqlSessionFactory.openSession(true);
                String d = "1997-01-08";
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Date date = sdf.parse(d);
                com.itheima.domain.Student stu = new com.itheima.domain.Student(3,"张三",19,date);
                sqlSession.update("StudentMapper.updateStudent",stu);
                sqlSession.close();
                is.close();
        }
}
